<?php

// +----------------------------------------------------------------------
// | Think.Admin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/Think.Admin
// +----------------------------------------------------------------------

namespace app\client\controller;

use controller\BasicClient;
use think\Controller;
use app\client\model\NodeModel;
use service\DataService;
use service\NodeService;
use service\ToolsService;
use service\LogService;
use think\Db;
use think\View;

/**
 * 后台入口
 * Class Index
 * @package app\client\controller
 */
class Index extends BasicClient
{

    public function _initialize()
    {
      // halt(session(''));
        parent::_initialize(); // TODO: Change the autogenerated stub
        // $this->user = session('c_user');
        // $this->uid = $this->user['create_by'];
    }


    /**
     * 后台框架布局
     * @return View
     */
    public function index()
    {
            NodeModel::applyAuthNode();
            $list = Db::name('ClientMenu')->where('status', '1')->order('sort asc,id asc')->select();
            $menus = ToolsService::arr2tree($list);
            // halt($menus);
            // halt(session('c_user.nodes'));
            //根据节点权限设置是否显示该栏目
            foreach($menus as $key=>$value){
                    // if(b_auth($menus[$key]['url'])){
                    //     $menus[$key]['show'] = '1';
                    // }else{
                    //     $menus[$key]['show'] = '0';
                    // }
                foreach($menus[$key]['sub'] as $kk=>$vv){
                    if(b_auth($menus[$key]['sub'][$kk]['url'])){
                        // halt($menus[$key]['sub'][$kk]);
                        // $menus[$key]['show'] = '1';
                        // $m[] = $menus[$key]['sub'][$kk]['url'];
                        // if(b_auth($menus[$key]['sub'][$kk]['url']) == '#'){
                            // $m2[] = $menus[$key]['sub'][$kk];
                            // $menus[$key]['show'] = '11';
                        // }else{
                            // $menus[$key]['show'] = '1';
                            $menus[$key]['sub'][$kk]['show'] = '1';
                        // }
                    }else{
                        // $menus[$key]['show'] = '0';
                        $menus[$key]['sub'][$kk]['show'] = '1';
                    }
                    if(!empty($menus[$key]['sub'][$kk]['sub'])){
                    foreach($menus[$key]['sub'][$kk]['sub'] as $kkk=>$vvv){
                        if(b_auth($menus[$key]['sub'][$kk]['sub'][$kkk]['url'])){
                            // $menus[$key]['show'] = '1';
                            $menus[$key]['sub'][$kk]['show'] = '1';
                            // $menus[$key]['sub'][$kk]['sub'][$kkk]['show'] = '1';
                        }else{
                            // $menus[$key]['show'] = '0';
                            $menus[$key]['sub'][$kk]['show'] = '1';
                            // $menus[$key]['sub'][$kk]['sub'][$kkk]['show'] = '0';
                        }
                    }
                    }
                    // if($menus[$key]['sub'][$kk]['url'] == '#'){
                        // halt($vv);
                    // }
                    // $node[] = $menus[$key]['sub'][$kk];
                }
            }
            // foreach ($menus[3]['sub'] as $k => $v) {
            //         if(!b_auth($v['url'])){
            //             $mm[$k]['show'] = '0';
            //         }else{
            //             $mm[$k]['show'] = '1';
            //             $mmm[] = $v['url'];
            //         }
            // }
            // halt($menus);
            // halt($menus[3]['sub']);
            // halt($m2);
            // halt($mm);
            // halt($m);
            // halt(session('c_user.nodes'));
            $this->assign('title', '系统管理');
            $this->assign('menus', $menus);

        return view();

    }

    /**
     * 主机信息显示
     * @return View
     */
    public function main() {
        $_version = Db::query('select version() as ver');
        $version = array_pop($_version);
        $this->assign('mysql_ver', $version['ver']);
//        if (session('user.username') === 'admin' && session('user.password') === '21232f297a57a5a743894a0e4a801fc3') {
//            $url = url('admin/index/pass') . '?id=' . session('user.id');
//            $alert = [
//                'type'    => 'danger',
//                'title'   => '安全提示',
//                'content' => "超级管理员默认密码未修改，建议马上<a href='javascript:void(0)' data-modal='{$url}'>修改</a>！"
//            ];
//            $this->assign('alert', $alert);
            $this->assign('title', '后台首页');
//        }
        return view();
    }

    /**
     * 修改密码
     *
     */
   public function pass() {
       if (intval($this->request->request('id')) !== intval(session('c_user.id'))) {
           $this->error('访问异常！');
       }
       if ($this->request->isGet()) {
           $this->assign('verify', true);
           return $this->_form('Client', 'pass');
       } else {
           $data = $this->request->post();
           if ($data['password'] !== $data['repassword']) {
               $this->error('两次输入的密码不一致，请重新输入！');
           }
           $user = Db::name('Client')->where('id', session('c_user.id'))->find();
           if (md5($data['oldpassword']) !== $user['password']) {
               $this->error('旧密码验证失败，请重新输入！');
           }
           if (DataService::save('Client', ['id' => session('c_user.id'), 'password' => md5($data['password'])])) {
            LogService::write('系统用户', '系统用户密码修改成功');
               $this->success('密码修改成功，下次请使用新密码登录！', '');
           } else {
               $this->error('密码修改失败，请稍候再试！');
           }
       }
   }

   /**
    * 修改资料
    */
   public function info() {
       if (intval($this->request->request('id')) === intval(session('c_user.id'))) {
           return $this->_form('Client', 'form');
       }
       $this->error('访问异常！');
   }

}
